#include "Ball.h"

Ball::Ball() : dx(2), dy(-3), canMove(true), acid(false), destroyed(false)
{
    ballSprite.SetTexture(rm->getTexture("ball2.png"));
    w = ballSprite.GetGlobalBounds().Width;
    h = ballSprite.GetGlobalBounds().Height;
    ballSprite.SetPosition(250, 400);
    pause();
}

Ball::~Ball()
{
    //dtor
}

void Ball::draw(sf::RenderWindow* window)
{
    window->Draw(ballSprite);
}

void Ball::reset()
{
    ballSprite.SetPosition(250, 400);
    dx = 2;
    dy = -3;
    pause();
    return;
}

void Ball::pause()
{
    canMove = false;
}

void Ball::start()
{
    canMove = true;
}

void Ball::update()
{
    if(canMove)
    {
        float x = ballSprite.GetGlobalBounds().Left;
        float y = ballSprite.GetGlobalBounds().Top;

        if(x + ballSprite.GetGlobalBounds().Width > WIDTH)
        {
            dx = -abso(dx);
            rm->playSound("Bouncey.wav");
        }
        else if(x < 0)
        {
            dx = abso(dx);
            rm->playSound("Bouncey.wav");
        }
        if(y < 0)
        {
            dy = abso(dy);
            rm->playSound("Bouncey.wav");
        }
        if(y > HEIGHT) //dead
        {
            destroyed = true;
        }
        ballSprite.SetPosition(x + dx, y + dy);
    }

    if(acidClock.GetElapsedTime().AsSeconds() > 4 && acid == true)
    {
        acid = false;
        ballSprite.SetTexture(rm->getTexture("ball2.png"));
    }
}

void Ball::makeAcid()
{
    acid = true;
    ballSprite.SetTexture(rm->getTexture("acidBall.png"));
    acidClock.Restart();
}
